<script setup>
import { nextTick, onActivated, onMounted, ref, watch } from "vue";
import { Local } from "../../utils/storage";
import { formatTime } from "../../utils/tool";
import { storeToRefs } from "pinia";
import { userStore } from "@/store/user";
import { getLikelist, getMusicDetailList, getPlayListAll, getUserlist } from "../../apis/user";
import { musicStore } from "@/store/music";
import { createWindow, ipcRendererSend } from "../../utils/ipcRenderer";
import { debounce } from "lodash";

const { loginStatus, userInfo, userId } = storeToRefs(userStore());
const { songListIds, musicDetail, songId, likeIdList } = storeToRefs(
  musicStore()
);
const songList = ref([]);

//获取喜欢歌曲数量
let loveId = ref();
const getInit = async () => {
  await getUserlist({ uid: userId.value }).then((res) => {
    loveId.value = res.playlist[0].id;
  });
  await getPlayListAll({ id: loveId.value }).then((res) => {
    songList.value = res.songs;
  });

  // let queryIds = "";
  // await getLikelist({ uid: userId.value }).then((res) => {
  //   queryIds = res.ids.join(",");
  // });
  // await getMusicDetailList({
  //   ids: queryIds,
  // }).then((res) => {
  //   songList.value = res.songs;
  // });
};

getInit();

const handlePlay = (item) => {
  musicDetail.value = item;
  songListIds.value = songList.value;
  Local.set("songList", songList.value);
  Local.set("musicInfo", item);
  songId.value = item.id;
  ipcRendererSend("windows-param", {
    emit: "playMusic",
    data: item.id,
    targetName: "home",
  });
};
const handleVideo = (item) => {
  createWindow({
    windowName: "videoDetail",
    path: "videoDetail",
    width: 1360,
    height: 820,
    param: { id: item.mv },
  });
  ipcRendererSend("windows-param", {
    emit: "playVideo",
    targetName: "home",
  });
};
const handleSinger = (id) => {
  createWindow({
    windowName: "singerDetail",
    path: "singerDetail",
    width: 1200,
    height: 820,
    param: { id: id },
  });
};
</script>

<template>
  <div class="user_love">
    <div class="head">
      <div class="cover">
        <el-image
          style="width: 45px; height: 45px"
          :src="userInfo.avatarUrl"
          fit="cover"
        ></el-image>
      </div>
      <div class="user_name">{{ userInfo.nickname }}喜欢的音乐</div>
    </div>
    <div v-if="songList.length > 0" class="song_list">
      <el-scrollbar height="100%">
        <div
          @dblclick="handlePlay(item)"
          :class="{ active_position: item.id === songId }"
          v-for="(item, index) in songList"
          :key="index"
          class="item"
        >
          <div class="num">{{ index + 1 }}</div>
          <div class="name">{{ item.name }}</div>
          <div class="album">
            <span @click="handleSinger(item.ar[0].id)">{{
              item.ar[0].name
            }}</span>
          </div>

          <div @click="getMusicLike(item.id, false, index)" class="btn">
            <i class="iconfont icon-aixin"></i>
            <el-button
              @click="handleVideo(item)"
              size="small"
              v-if="item.mv != 0"
              type="primary"
              >mv</el-button
            >
          </div>
          <div class="timer">{{ formatTime(item.dt) }}</div>
        </div>
        <upbar></upbar>
      </el-scrollbar>
    </div>
    <el-skeleton animated v-else style="margin: 30px" :rows="13"> </el-skeleton>
  </div>
</template>

<style scoped lang="scss">
.user_love {
  height: 100%;
  .head {
    display: flex;
    align-items: center;
    .cover {
      .el-image {
        border-radius: 50%;
      }
    }
    .user_name {
      margin-left: 20px;
      font-size: 40px;
      font-weight: 600;
    }
  }
  .song_list {
    margin-top: 20px;
    height: calc(100% - 100px);
    .item {
      height: 50px;
      display: flex;
      align-items: center;
      border-radius: 8px;
      cursor: pointer;
      &:hover {
        background-color: #f2f2f4;
      }
      .num {
        width: 50px;
        text-align: center;
        font-size: 16px;
        //   font-weight: 600;
      }
      .name {
        width: 40%;
        font-size: 18px;
        font-weight: 600;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .album {
        flex: 1;
        font-size: 15px;
        font-weight: 600;
        &:hover {
          text-decoration: underline;
        }
      }
      .btn {
        font-size: 15px;
        font-weight: 600;
        width: 10%;
        color: #335eea;

        .el-button {
          margin-left: 6px;
          background-color: #fff;
          color: rgb(16, 24, 134);
          font-weight: 600;
          border: none;
          border-radius: 5px;
          &:hover {
            transform: scale(1.1);
          }
        }
        i {
          margin-right: 20px;
          &:hover {
            transform: scale(1.1);
          }
        }
      }
      .timer {
        width: 50px;
        margin-right: 16px;
        text-align: center;
        font-size: 18px;
        color: rgb(167, 157, 157);
      }
    }
    .active_position {
      color: #335eea;
      background: #eaeffd;

      .num {
        color: #335eea;
      }
      .album,
      .name,
      .timer {
        color: #335eea;
      }
    }
  }
}
</style>
